package cn.gok.client.controller;

import cn.gok.client.service.CustomerService;
import cn.gok.common.entity.customer.CustomerConsignee;
import cn.gok.common.entity.customer.CustomerInfo;
import cn.gok.common.entity.customer.CustomerLogin;
import cn.gok.common.enums.ResultEnum;
import cn.gok.common.resp.Result;
import cn.gok.server.service.CustomerLoginLogService;
import cn.gok.utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author shikundai
 * @date 2020/11/1
 * @time 16:23
 * @description 顾客控制器
 */
@RestController
@RequestMapping("/client/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerLoginLogService customerLoginLogService;
    /**
     * @author shikundai
     * @date 2020/11/1
     * @time 16:24
     * @description  顾客注册
     */
    @PostMapping("/register")
    public Result register(CustomerLogin customerLogin, CustomerInfo customerInfo, CustomerConsignee customerConsignee){
        Result result=new Result();

        boolean out=customerService.addCustomer(customerLogin,customerInfo,customerConsignee);

        if (out){
            result=ResultUtil.success("");
        }else {
            result=ResultUtil.info(ResultEnum.SYSTEM_UNKNOWN_ERROR.getCode(),"用户注册失败！","");
        }

        return result;
    }

    /**
     * @author shikundai
     * @date 2020/11/1
     * @time 17:11
     * @description  用户登录操作
     */
    @PostMapping("/login")
    public Result login(HttpServletRequest req,CustomerLogin customerLogin){
        Result result=new Result();
       boolean out =customerService.login(req,customerLogin);
       if (out){
           return ResultUtil.success("");
       }else {
           return ResultUtil.info(ResultEnum.SYSTEM_UNKNOWN_ERROR.getCode(),"登录失败！","");
       }

    }
    /**
     * @author shikundai
     * @date 2020/11/9
     * @time 9:44
     * @description 顾客登出
     */
    @GetMapping("/logOut")
    public Result logOut(HttpServletRequest request){
        request.getSession().removeAttribute("customerId");
        return ResultUtil.success("");
    }

    /**
     * @author shikundai
     * @date 2020/11/1
     * @time 17:53
     * @description  判断用户是否登录
     */
    @GetMapping("/isLogin")
    public Result isLogin(HttpServletRequest req){
        Result result=new Result();
        Object obj=req.getSession().getAttribute("customerId");
        if (obj!=null){
            Map map=customerService.getCustomerMsg(String.valueOf(obj));
            System.out.println(JSON.toJSONString(map));
            result= ResultUtil.success(map);
        }
        return result;
    }

    /**
     * @author shikundai
     * @date 2020/11/9
     * @time 1:06
     * @description 顾客个人信息修改保存
     */
    @PostMapping("/saveCustomerInfo")
    public Result saveCustomerInfo(CustomerInfo customerInfo){
        System.out.println(customerInfo);
        boolean out=customerService.updateCustomer(customerInfo);
        if (out){
            return ResultUtil.success("");
        }else {
            return ResultUtil.error("");
        }
    }

    /**
     * @author shikundai
     * @date 2020/11/9
     * @time 9:35
     * @description 顾客密码修改
     */
    @PostMapping("/updatePsw")
    public Result updatePsw(@RequestParam("oldPsw") String oldPsw,@RequestParam ("psw")String psw,@RequestParam("customerId")String customerId,HttpServletRequest request){
        boolean out=customerService.updatePsw(customerId,oldPsw,psw);
        if (out){
            //移除登录信息
            logOut(request);
            return ResultUtil.success("更新成功！");
        }else {
            return ResultUtil.error("");
        }

    }


    /**
     * 描述：获取顾客金额消费记录
     * @author shikundai
     * @date 2020/12/11
     * @time 10:45
     */
    @GetMapping("/getCustomerBalance")
    @ResponseBody
    public Result getCustomerBalance(@RequestParam("time")String time,@RequestParam("page") int page,@RequestParam("pageSize")int pageSize){
        PageInfo pageInfo=customerService.getCustomerBalanceLog(time,page,pageSize);
        return ResultUtil.success(pageInfo);
    }


    /**
     * @author shikundai
     * @date 2020/11/1
     * @time 17:11
     * @description  处理运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result Exception(Exception e){
        Result result=ResultUtil.error("");
        result.setMsg(e.getMessage());
        return result;
    }






}
